<?php

namespace App\Http\Controllers\v1;

use App\Craftsman;
use App\Project;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index(Request $request)
    {
        $data = Project::orderBy('sort','ASC')->get()->each(function (&$item){
            $item['child'] = Craftsman::where('project_id',$item->id)->orderBy('created_at','ASC')->first();
        });
        $index = 0;
        $res = [];
        foreach ($data as $key=>$value){
            $key++;
            if ($key%2==0){
                array_push($res[$index],$value);
                $index++;
            }else{
                $res[$index] = [];
                array_push($res[$index],$value);
            }
        }
        return $this->success($res);
    }
}
